package cn.sofwin.os.service.impl;

import cn.sofwin.core.mvc.support.annotation.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import cn.sofwin.core.mvc.service.impl.BaseServiceImpl;
import cn.sofwin.core.mvc.support.bean.info.Module;
import cn.sofwin.os.dao.GoodsDao;
import cn.sofwin.os.entity.Goods;
import cn.sofwin.os.entity.UserOrder;
import cn.sofwin.os.service.UserOrderService;
import cn.sofwin.os.dao.UserOrderDao;

import java.util.Date;

/**
 * 用户订单服务层
 * @author LetCode
 * @version 1.0
 * @date 2020-04-23 11:28:34
 *
 */
@CacheConfig(cacheNames = "os_userOrder")
@Service("userOrderService")
public class UserOrderServiceImpl extends BaseServiceImpl<UserOrder> implements UserOrderService {
	
	@BaseDao
	UserOrderDao userOrderDao;

	@Autowired
	GoodsDao goodsDao;


	@Cacheable
	@Override
	public Module loadModuleInfo(){
		Module module = new Module("用户订单", "userOrder");

		// 增加表头字段信息
		module.addColumn("id", "序号")
		      .addColumn("total", "商品数量")
		      .addColumn("amount", "订单金额")
		      .addColumn("addTime", "创建时间")
		      .addColumn("payTime", "支付时间")
		      .addColumn("finishTime", "完成时间")
		      .addColumn("status", "订单状态：-1.删除，0.冻结，1.等待支付，2.等待发货，3.等待收货，4.等待完成，5.订单完成，6.申请售后");

		// 增加搜索字段信息
		module.addSearchField("title", "名称");

		// 增加视图地址
		module.addView("list", "/home/list/userOrder")
		      .addView("form", "/home/form/userOrder");

		// 增加数据请求接口
		module.addApi("base", "/os/userOrder");

		return module;
	}

	@Override
	public UserOrder create(UserOrder order) {
		// 计算订单金额
		Goods goods = goodsDao.selectOne(order.getGoods().getId());
		Integer total = order.getTotal();
		if(total > 0){
			order.setAmount(goods.getPrice() * total);
			order.setAddTime(new Date()); // 设置订单添加时间
			userOrderDao.insert(order);
		}else{
			System.out.println("恶意订单，商品数量：" + total);
		}

		return order;
	}
}